Skip to main content

Lists

a### 1. Introduction to Lists List is a collection of items that are stored in a specific order.

  • We can access individual elements in the list by indexing it just like we can with a string.
  • We can also get the number of elements within the list by calling the len() function.
my_list = [1, 2, 3]

print(len(my_list)) # Output: 3

print(my_list[0]) # Output: 1
print(my_list[1]) # Output: 2
print(my_list[2]) # Output: 3

Unlike strings, lists are mutable, we can modify elements within a list.

my_list = [1, 2, 3, 4, 5]

my_list[0] = 10

print(my_list[0]) # Output: 10

Lists can also store data of different types, they can store everything from strings to other lists.

my_list = ["I", "am", "a", "list"]

print(my_list[0]) # Output: I
print(my_list[1]) # Output: am
print(my_list[2]) # Output: a
print(my_list[3]) # Output: list

We can even mix and match different types of elements in a list. This is generally not recommended, but it is possible.

my_list = [1, "Hello", 3.14, True]

2. Check for Empty Lists​

1. len() function​

my_list = [1, 2, 3]

if len(my_list) > 0:
print("The list is not empty")
else:
print("The list is empty")

# equivalent statement
if my_list:
print("The list is not empty")
else:
print("The list is empty")

2. in and not in​

We can also use the in operator to check if an element is present in a list:

my_list = [1, 2, 3]

if 2 in my_list:
print("2 is in the list")
else:
print("2 is not in the list")

To check if an element is not in the list, we can use the not in operator:

my_list = [1, 2, 3]

if 4 not in my_list:
print("4 is not in the list")
else:
print("4 is in the list")

3. List Looping​

We can also loop through lists similar to how we loop through strings, either by using the length of the list, or the in operator.

my_list = [1, 2, 3, 4, 5]

length = len(my_list)

for i in range(length):
print(my_list[i])

# using `in` operator
for element in my_list:
print(element)

4. List Functions​

1. sum() function​

The sum() function returns the sum of all the elements in the list.

2. min() function​

The min() function returns the smallest element in the list.

3. max() function​

The max() function returns the largest element in the list.

Example:
my_list = [1, 2, 3, 4, 5]

print(sum(my_list)) # Output: 15
print(min(my_list)) # Output: 1
print(max(my_list)) # Output: 5

5. List Methods​

1. append()​

We can also add new elements to a list using the append() method. The method modifies the list in-place by adding an element to the end.

my_list = [1, 2, 3]
print(my_list) # Output: [1, 2, 3]

my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]

2. pop()​

By default, the pop() method removes the last element from the list. We can also specify an index to remove a specific element. The pop() method will also return this removed element.

my_list = [1, 2, 3]

my_list.pop()
print(my_list) # Output: [1, 2]
my_list = [1, 2, 3]

# specify an index
first_element = my_list.pop(0)
print(my_list) # Output: [2, 3]
print(first_element) # Output: 1

my_list.pop(0)
print(my_list) # Output: [3]

3. index()​

The index() returns the index of the first occurrence of the specified element in the list. If the element is not found, it raises a ValueError.

my_list = [1, 2, 3, 4, 5, 3]

print(my_list.index(3)) # Output: 2

6. List Slicing​

We can extract a portion of a list by specifying a start and end index. The syntax is similar to slicing strings.

  • If we omit the start index, it will default to the beginning of the list.
  • If we omit the end index, it will default to the end of the list.
  • We can also specify a negative step, after two colons, to reverse the list. None of these operations modify the original list. They only return a new list with the specified slice.
my_list = [1, 2, 3, 4, 5]

print(my_list[1:3]) # Output: [2, 3]

print(my_list[:3]) # Output: [1, 2, 3]

print(my_list[2:]) # Output: [3, 4, 5]

print(my_list[::-1]) # Output: [5, 4, 3, 2, 1]

Using negative index​

Using -1 to access an element in a list will give us the last element, -2 will give us the second-to-last element, and so on.

my_list = [1, 2, 3, 4, 5]

print(my_list[-1]) # Output: 5
print(my_list[-2]) # Output: 4
print(my_list[-3]) # Output: 3

7. Tuples​

Tuples are very similar to lists, but they have one key difference: they are immutable. This means that once a tuple is created, it cannot be changed. We can create a tuple by using parentheses instead of square brackets:

my_tuple = (4, 5, 6)

print(my_tuple) # Output: (4, 5, 6)

Since we can't modify a tuple, the following code will raise an error:

my_tuple = (4, 5, 6)

my_tuple[0] = 1 # Raises an error

1. Accessing Tuple Elements​

We can index a tuple just like a list:

my_tuple = (4, 5, 6)

print(my_tuple[0]) # Output: 4
print(my_tuple[1]) # Output: 5
print(my_tuple[2]) # Output: 6

2. Tuple Slicing​

We can also use slicing on a tuple. Slicing a tuple doesn't modify it, instead it creates a new tuple with the specified slice.

my_tuple = (4, 5, 6)

print(my_tuple[1:]) # Output: (5, 6)

We also can't call append or pop on a tuple, since these functions would modify it. We can however still call sum(), max(), and min() on a tuple, since these functions don't modify the tuple.